using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class ClienteDao : DaoBase
    {
        public ClienteDao(string strConn) : base(strConn)
        {
        }

        ClienteHelper helper = new ClienteHelper();

        public string Save(Cliente entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpSave);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, entity.ID);
            dbProvider.AddInParameter(command, helper.Ktokd, DbType.String, entity.Ktokd);
            dbProvider.AddInParameter(command, helper.Anred, DbType.String, entity.Anred);
            dbProvider.AddInParameter(command, helper.Name1, DbType.String, entity.Name1);
            dbProvider.AddInParameter(command, helper.Name2, DbType.String, entity.Name2);
            dbProvider.AddInParameter(command, helper.Stras, DbType.String, entity.Stras);
            dbProvider.AddInParameter(command, helper.StrSuppl1, DbType.String, entity.StrSuppl1);
            dbProvider.AddInParameter(command, helper.StrSuppl2, DbType.String, entity.StrSuppl2);
            dbProvider.AddInParameter(command, helper.Ort01, DbType.String, entity.Ort01);
            dbProvider.AddInParameter(command, helper.Ort02, DbType.String, entity.Ort02);
            dbProvider.AddInParameter(command, helper.Land1, DbType.String, entity.Land1);
            dbProvider.AddInParameter(command, helper.Regio, DbType.String, entity.Regio);
            dbProvider.AddInParameter(command, helper.Telf1, DbType.String, entity.Telf1);
            dbProvider.AddInParameter(command, helper.Telf2, DbType.String, entity.Telf2);
            dbProvider.AddInParameter(command, helper.Knurl, DbType.String, entity.Knurl);
            dbProvider.AddInParameter(command, helper.Stcd1, DbType.String, entity.Stcd1);
            dbProvider.AddInParameter(command, helper.Stcdt, DbType.String, entity.Stcdt);
            dbProvider.AddInParameter(command, helper.Kukla, DbType.String, entity.Kukla);

            return (string)dbProvider.ExecuteScalar(command);
        }

        public void Update(Cliente entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, entity.ID);
            dbProvider.AddInParameter(command, helper.Ktokd, DbType.String, entity.Ktokd);
            dbProvider.AddInParameter(command, helper.Anred, DbType.String, entity.Anred);
            dbProvider.AddInParameter(command, helper.Name1, DbType.String, entity.Name1);
            dbProvider.AddInParameter(command, helper.Name2, DbType.String, entity.Name2);
            dbProvider.AddInParameter(command, helper.Stras, DbType.String, entity.Stras);
            dbProvider.AddInParameter(command, helper.StrSuppl1, DbType.String, entity.StrSuppl1);
            dbProvider.AddInParameter(command, helper.StrSuppl2, DbType.String, entity.StrSuppl2);
            dbProvider.AddInParameter(command, helper.Ort01, DbType.String, entity.Ort01);
            dbProvider.AddInParameter(command, helper.Ort02, DbType.String, entity.Ort02);
            dbProvider.AddInParameter(command, helper.Land1, DbType.String, entity.Land1);
            dbProvider.AddInParameter(command, helper.Regio, DbType.String, entity.Regio);
            dbProvider.AddInParameter(command, helper.Telf1, DbType.String, entity.Telf1);
            dbProvider.AddInParameter(command, helper.Telf2, DbType.String, entity.Telf2);
            dbProvider.AddInParameter(command, helper.Knurl, DbType.String, entity.Knurl);
            dbProvider.AddInParameter(command, helper.Stcd1, DbType.String, entity.Stcd1);
            dbProvider.AddInParameter(command, helper.Stcdt, DbType.String, entity.Stcdt);
            dbProvider.AddInParameter(command, helper.Kukla, DbType.String, entity.Kukla);

            dbProvider.ExecuteNonQuery(command);
        }

        public void Delete(System.String id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDelete);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, id);

            dbProvider.ExecuteNonQuery(command);
        }

        public Cliente GetById(System.String id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetById);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, id);
            Cliente entity = null;

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }

            return entity;
        }

        public List<Cliente> List()
        {
            List<Cliente> entitys = new List<Cliente>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<Cliente> GetByCriteria(string codigo, string nombre, string tipoDoc, string numDoc)
        {
            List<Cliente> entitys = new List<Cliente>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, codigo);
            dbProvider.AddInParameter(command, helper.Name1, DbType.String, nombre);
            dbProvider.AddInParameter(command, helper.Stcdt, DbType.String, tipoDoc);
            dbProvider.AddInParameter(command, helper.Stcd1, DbType.String, numDoc);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public Cliente ObtenerInterlocutor(string codigo, string parvw)
        {
            Cliente entity = null;
            DbCommand command = dbProvider.GetStoredProcCommand("USP_OBTENER_DESTINATARIO_MERCANCIA");

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, codigo);
            dbProvider.AddInParameter(command, "PARVW", DbType.String, parvw);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }
            return entity;
        }

    }
}
